package com.easyspringboot.bulider;
import com.easyspringboot.bean.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author xiaoyi
 * @Version 1.0
 * @ClassName BuildBase
 * @Time 17:01
 */
public class BuildBase {
    private static Logger logger = LoggerFactory.getLogger(BuildBase.class);

    public static void execute() {
        List<String> headerInfoList = new ArrayList<>();

        //生成date枚举
        headerInfoList.add("package " + Constants.PACKAGE_ENUM + ";");
        build(headerInfoList,"DateTimeEnum", Constants.PATH_ENUM);
        headerInfoList.clear();

        headerInfoList.add("package " + Constants.PACKAGE_UTILS + ";");
        build(headerInfoList,"DateUtils", Constants.PATH_UTILS);
        headerInfoList.clear();
        //生成BaseMapper
        headerInfoList.add("package " + Constants.PACKAGE_MAPPER + ";");
        build(headerInfoList,"BaseMapper", Constants.PATH_MAPPER);
        //生成PageSize枚举
        headerInfoList.clear();
        headerInfoList.add("package " + Constants.PACKAGE_ENUM + ";");
        build(headerInfoList,"PageSize", Constants.PATH_ENUM);
        //生成SimplePage
        headerInfoList.clear();
        headerInfoList.add("package " + Constants.PACKAGE_QUERY + ";");
        headerInfoList.add("import " + Constants.PACKAGE_ENUM + ".PageSize;");
        build(headerInfoList,"SimplePage", Constants.PATH_QUERY);
        //生成BaseQuery
        headerInfoList.clear();
        headerInfoList.add("package " + Constants.PACKAGE_QUERY + ";");
        build(headerInfoList,"BaseQuery", Constants.PATH_QUERY);
        //生成PaginationResultVO
        headerInfoList.clear();
        headerInfoList.add("package " + Constants.PACKAGE_VO + ";");
        build(headerInfoList,"PaginationResultVO", Constants.PATH_VO);
        //生成ResponseVO
        headerInfoList.clear();
        headerInfoList.add("package " + Constants.PACKAGE_VO + ";");
        build(headerInfoList,"ResponseVO", Constants.PATH_VO);
        //生成ExceptionEnum
        headerInfoList.clear();
        headerInfoList.add("package " + Constants.PACKAGE_EXCEPTION + ";");
        headerInfoList.add("import " + Constants.PACKAGE_ENUM + ".ResponseCodeEnum;");
        build(headerInfoList,"BusinessException", Constants.PATH_EXCEPTION);
        //生成ResponseCodeEnum
        headerInfoList.clear();
        headerInfoList.add("package " + Constants.PACKAGE_ENUM + ";");
        build(headerInfoList,"ResponseCodeEnum", Constants.PATH_ENUM);
        //生成BaseController
        headerInfoList.clear();
        headerInfoList.add("package " + Constants.PACKAGE_CONTROLLER + ";");
        headerInfoList.add("import " + Constants.PACKAGE_ENUM + ".ResponseCodeEnum;");
        headerInfoList.add("import " + Constants.PACKAGE_VO + ".ResponseVO;");
        build(headerInfoList,"ABaseController", Constants.PATH_CONTROLLER);
        //GlobalExceptionHandlerController
        headerInfoList.clear();
        headerInfoList.add("package " + Constants.PACKAGE_CONTROLLER + ";");
        headerInfoList.add("import " + Constants.PACKAGE_EXCEPTION + ".BusinessException;");
        headerInfoList.add("import " + Constants.PACKAGE_ENUM + ".ResponseCodeEnum;");
        headerInfoList.add("import " + Constants.PACKAGE_VO + ".ResponseVO;");
        build(headerInfoList,"AGlobalExceptionHandlerController", Constants.PATH_CONTROLLER);



    }

    private static void build(List<String> headerInfoList,String fileName, String outPutPath) {
        File file = new File(outPutPath);
        if (!file.exists()) {
            file.mkdirs();
        }
        //创建文件
        File javaFile = new File(outPutPath, fileName + ".java");//java文件名
        OutputStream outputStream = null;//字节流
        OutputStreamWriter outputStreamWriter = null;//字符流
        BufferedWriter bufferedWriter = null;//缓冲流

        InputStream inputStream = null;//字节流
        InputStreamReader inputStreamReader = null;//字符流
        BufferedReader bufferedReader = null;//缓冲流
        try {
            //创建文件
            outputStream = new FileOutputStream(javaFile);
            outputStreamWriter = new OutputStreamWriter(outputStream, "utf8");
            bufferedWriter = new BufferedWriter(outputStreamWriter);
            //读取模板文件
            String templatePath = BuildBase.class.getClassLoader().getResource("template/" + fileName + ".txt").getPath();
            //读取模板文件
            inputStream = new FileInputStream(templatePath);
            inputStreamReader = new InputStreamReader(inputStream, "utf8");
            bufferedReader = new BufferedReader(inputStreamReader);
            for (String headerInfo : headerInfoList) {
                bufferedWriter.write(headerInfo);
                bufferedWriter.newLine();
                bufferedWriter.newLine();
            }

            String lineInfo = null;
            while ((lineInfo = bufferedReader.readLine()) != null) {
                bufferedWriter.write(lineInfo);
                bufferedWriter.newLine();
            }
            bufferedWriter.flush();

        } catch (Exception e) {
            logger.error("生成基础类：{},创建文件失败", fileName, e);

        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (Exception e) {
                    logger.error("生成基础类：{},关闭缓冲流失败", fileName, e);
                }
            }
            if (inputStreamReader != null) {
                try {
                    inputStreamReader.close();
                } catch (Exception e) {
                    logger.error("生成基础类：{},关闭字符流失败", fileName, e);
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (Exception e) {
                    logger.error("生成基础类：{},关闭字节流失败", fileName, e);
                }
            }
            if (bufferedWriter != null) {
                try {
                    bufferedWriter.close();
                } catch (Exception e) {
                    logger.error("生成基础类：{},关闭缓冲流失败", fileName, e);
                }
            }
            if (outputStreamWriter != null) {
                try {
                    outputStreamWriter.close();
                } catch (Exception e) {
                    logger.error("生成基础类：{},关闭字符流失败", fileName, e);
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (Exception e) {
                    logger.error("生成基础类：{},关闭字节流失败", fileName, e);
                }
            }


        }


    }
}

